﻿using System;
using System.Drawing;

namespace MDAControlsFramework.Helpers {
	/// <summary>
	/// Uchová informace o stylu prvku
	/// </summary>
	public class MDAStyle: IMDAObject {
		// nové style údaje
		private MDABackground _background = null;
		private MDAText _text = null;
		private MDATextStyle _textStyle = null;
		private Image _image = null;
		private MDABorder _border = null;
		private bool _selected = false;
		private string _name = "null";

		public static MDAStyle None;

		public MDAStyle() {
		}

		public MDAStyle(string name) {
			_name = name;
		}

		public MDAStyle(MDABackground background, MDAText text, MDATextStyle textStyle, Image image, MDABorder border) {
			_background = background;
			_text = text;
			_textStyle = textStyle;
			_image = image;
			_border = border;
		}

		public object Clone() {
			return this.DeepCopy();
		}

		public void CopyData(MDAStyle style) {
			_name = style.Name;
			if (style.Background != null) {
				_background = (MDABackground)style.Background.Clone();
			}
			if (style.Text != null) {
				_text = style.Text.DeepCopy();
			}
			if (style.TextStyle != null) {
				_textStyle = style.TextStyle;
			}
			if (style.Image != null) {
				_image = style.Image;
			}
			if (style.Border != null) {
				_border = style.Border;
			}
		}

		public MDAStyle DeepCopy() {
			MDAStyle style = new MDAStyle();
			style.CopyData(this);
			return style;
		}

		#region properties

		public MDABackground Background {
			get {
				return _background;
			}
			set {
				_background = value;
			}
		}

		public MDAText Text {
			get {
				return _text;
			}
			set {
				_text = value;
			}
		}

		public Image Image {
			get {
				return _image;
			}
			set {
				_image = value;
			}
		}

		public MDABorder Border {
			get {
				return _border;
			}
			set {
				_border = value;
			}
		}

		public MDATextStyle TextStyle {
			get {
				return _textStyle;
			}
			set {
				_textStyle = value;
			}
		}

		/// <summary>
		/// Zda je tento styl určen jako vybraný
		/// </summary>
		public bool Selected {
			get {
				return _selected;
			}
			set {
				_selected = value;
			}
		}

		public string Name {
			get {
				return _name;
			}
			set {
				_name = value;
			}
		}

		#endregion

		#region Draw

		public void DrawBackground(Graphics g, Rectangle rectangle) {
			if (_background != null && _background != MDABackground.None) {
				_background.Draw(g, rectangle);
			}
		}

		public void DrawImage(Graphics g, Rectangle rectangle) {
			if (_image != null) {
				DrawImage(g, rectangle, _image);
			}
		}

		public void DrawText(Graphics g, Rectangle rectangle) {
			if (_text != null && _text != MDAText.None) {
				if (_textStyle != null) {
					_text.Draw(g, _textStyle, rectangle);
				}
			}
		}

		public void DrawBorder(Graphics g, Rectangle rectangle) {
			if (_border != null && _border != MDABorder.None) {
				_border.Draw(g, rectangle);
			}
		}

		public void DrawBackground(Graphics g, Rectangle rectangle, MDAStyle style) {
			if (_background == MDABackground.None) {
				return;
			}

			if (_background != null) {
				_background.Draw(g, rectangle);
			} else {
				style.DrawBackground(g, rectangle);
			}
		}

		public void DrawImage(Graphics g, Rectangle rectangle, MDAStyle style) {
			if (_image != null) {
				DrawImage(g, rectangle, _image);
			} else {
				if (style.Image != null) {
					DrawImage(g, rectangle, style.Image);
				}
			}
		}

		public void DrawText(Graphics g, Rectangle rectangle, MDAStyle style) {
			if (_text == MDAText.None || _textStyle == MDATextStyle.None) {
				return;
			}

			if (_text != null) {
				if (_textStyle != null) {
					_text.Draw(g, _textStyle, rectangle);
				} else {
					_text.Draw(g, style.TextStyle, rectangle);
				}
			} else {
				if (style.Text != null) {
					if (_textStyle != null) {
						style.Text.Draw(g, _textStyle, rectangle);
					} else {
						style.Text.Draw(g, style.TextStyle, rectangle);
					}
				}
			}
		}

		public void DrawBorder(Graphics g, Rectangle rectangle, MDAStyle style) {

			if (_border != null) {
				_border.Draw(g, rectangle);
			} else {
				if (style.Border != null) {
					style.DrawBorder(g, rectangle);
				}
			}
		}

		private void DrawImage(Graphics g, Rectangle clipRectangle, Image image) {
			// TODO, pridat orezavani obrazku, pripadne centrovani do obdelniku
			g.DrawImage(image, clipRectangle.X, clipRectangle.Y);
		}

		#endregion

		public string GetName() {
			return _name;
		}

		#region settings

		public bool AddObject(IMDAObject objectInstance) {
			if (objectInstance.GetType() == typeof(MDABackground)) {
				_background = objectInstance as MDABackground;
				return true;
			}

			if (objectInstance.GetType() == typeof(MDABorder)) {
				_border = objectInstance as MDABorder;
				return true;
			}

			if (objectInstance.GetType() == typeof(MDATextStyle)) {
				if (_textStyle == null) {
					_textStyle = objectInstance as MDATextStyle;
				} else {
					_textStyle.Update((MDATextStyle)objectInstance);
				}
				return true;
			}

			return true;
		}

		public bool SetProperty(string name, string value) {
			if (name == "text") {
				if (_text == null) {
					_text = new MDAText();
				}
				_text.Text = value;
				return true;
			}

			if (name == "name") {
				_name = value;
				return true;
			}

			if (name == "align") {
				if (_textStyle == null) {
					_textStyle = new MDATextStyle();
				}
				this._textStyle.SetProperty(name, value);
				return true;
			}

			if (name == "color") {
				if (_textStyle == null) {
					_textStyle = new MDATextStyle();
				}
				this._textStyle.SetProperty(name, value);
				return true;
			}

			if (name.StartsWith("background")) {
				if (_background == null) {
					_background = new MDABackground();
				}
				return _background.SetProperty(name, value);
			}

			if (name.StartsWith("border")) {
				if (_border == null) {
					_border = new MDABorder();
				}
				return _border.SetProperty(name, value);
			}

			return false;
		}

		public void AddEvent(string eventName, string eventHandler) {
			return;
		}

		#endregion

		public override string ToString() {
			return "{Style \"" + this._name + "\" " + Convert.ToString(_text) + " " + Convert.ToString(_textStyle) + " "
				+ Convert.ToString(_background) + " " + Convert.ToString(_border) + "}";
		}
	}
}